from time import sleep
from selenium.webdriver.common.by import By
import random
random_number = random.randint(1, 3)
from base.base_page import BasePage

class Shop_Cat(BasePage):

    VIEW=(By.XPATH,f'//div[@class="home"]/div[5]//div[@class="clear"]/div[3]/a')  #浏览新品第一个商品
    ADD_CAT=(By.XPATH,'//button/span[text()="加入购物车"]') #加入购物车
    SHOPIN_CAT=(By.CSS_SELECTOR,'div.el-badge>a') #购物车按钮
    GOUWU_NUMBER=(By.XPATH,'//font[@class="topay2"]')#购物车数量
    KUCUN_BUZU=(By.XPATH,'//p[@class="el-message__content"]') #库存不足提示
    INPUT_CAT=(By.XPATH,'//div[@class="el-input el-input--mini"]/input')  #数量填写文本框
    KU_CUN=(By.XPATH,'//span[@class="goodsunit"]/span')#商品库存有多少件
    CAT_NUMBER = (By.XPATH, '//div[@class="float-bar-right"]/div/font')  # 购物车已选择商品的数量
    SHANG_PIN = (By.XPATH, '//td[@class="f-red font-big"]/span[1]')  # 详情页商品价格
    CAT_SUM=(By.XPATH,'//div[@class="float-bar-right"]/button')#购物车结算按钮
    CAT_JAIGE=(By.XPATH,'//div[@class="float-bar-right"]/div[2]/span')# 购物车价格
    QUAN_XUAN=(By.CSS_SELECTOR,'span.el-checkbox__inner') #全选按钮
    PI_SHANCHU=(By.XPATH,'//div[@class="float-bar-left"]/button') #批量删除按钮
    PI_SHANCHU_QUE=(By.XPATH,'//div[@class="el-message-box__btns"]/button[2]/span') #确认按钮
    SHOU_ADD=(By.XPATH,'//div[@class="address_chose_title"]/i') #收货地址添加按钮
    SHOU_HUOREN=(By.XPATH,'//form[@class="el-form"]/div[1]/div/div/input')#收货人
    SHOU_IPHONE=(By.XPATH,'//form[@class="el-form"]/div[2]/div/div/input')#手机号
    SHI=(By.XPATH,'//span[@class="el-cascader-node__label"][text()="北京市"]')#北京市
    SHIQU=(By.XPATH,'//span[@class="el-cascader-node__label"][text()="市辖区"]')#市辖区
    SHIQUDI=(By.XPATH,'//span[@class="el-cascader-node__label"][text()="东城区"]')#东城区
    SHOU_DIZHI=(By.CSS_SELECTOR,'.el-input--suffix > input:nth-child(1)') #地址
    SHOU_XIANGXI=(By.XPATH,'//form[@class="el-form"]/div[4]/div/div/input') #详细地址
    #选择地址、添加地址确认按钮、支付方式、支付完成、
    SHOU_BUTTON=(By.XPATH,'//span[@class="dialog-footer"]/button[2]')# 确认保存
    SHOU_XUANZE=(By.XPATH,'//div[@class="address_list"]/ul/li[1]')#选择下单地址
    TI_JIAODINGDAN=(By.CSS_SELECTOR,'.foot_create_order_btn>button>span') #提交订单按钮
    TI_ZHIFU=(By.XPATH,'//div[@class="total_price"]')#支付金额
    TI_ZHIFU_WHEAT=(By.CSS_SELECTOR,'li:nth-child(1) > div > img')#W微信支付
    ZHIFU_ZHUANGTAI=(By.XPATH,'//div[@class="user_index_order_list"]/div[1]/div[1]/div[1]')#支付状态
    SHOU_YE=(By.XPATH,'//div[@class="logo"]/a/div')#首页


    def page_view(self):#滑动到指定位置
        self.scrollIntoView(self.VIEW,page_name='浏览商品')
        self.click(self.VIEW,page_name='点击进入详情页')
        self.switch_wind()
    def switch_wind(self): #切换到商品详情页
        ele = self.all_headers_windows()
        ll=self.driver.switch_to.window(ele[-1])

    def switch_wind_end(self): #判断窗口==3
        ele = self.all_headers_windows()
        if len(ele)==3:
            ll=self.driver.switch_to.window(ele[-1])
            self.driver.close()
    def gouwuce(self):
        return self.get_text(self.GOUWU_NUMBER,page_name='获取购物车数量')
    def commod_vie_swith(self): #滑动-切换窗口
        self.switch_wind()
        self.page_view()

    def commod_vie_swith2(self):#滑动-切换窗口-加入购物车-进入购物车
        self.switch_wind()
        self.page_view()
        self.commodity_add_cat()
        self.shopping_cart()

    def commodity_add_cat(self):#商品详情页-加入购物车
        sleep(1)
        self.click(self.ADD_CAT,page_name='点击加入购物车')

    def commodity_cucun_buzu(self):
        return self.get_text(self.KUCUN_BUZU,page_name='库存不足提示')
    def commodity_kucun(self):#商品详情页库存
        ele=self.get_text(self.KU_CUN,page_name='获取商品详情页库存')
        return ele

    def commodity_input(self,number): #清空-输入数-加入购买
        self.clear(self.INPUT_CAT,page_name='清空输入框')
        self.input_element(self.INPUT_CAT,number,page_name=f'输入购买数量:{number}')
        self.click(self.ADD_CAT, page_name='点击加入购物车')

    def commodity_input2(self,number): #清空-输入数-加入购买
        self.clear(self.INPUT_CAT,page_name='清空输入框')
        self.input_element(self.INPUT_CAT,number,page_name=f'输入购买数量:{number}')
        self.click(self.ADD_CAT, page_name='点击加入购物车')
        sleep(5)
        self.click(self.ADD_CAT, page_name='点击加入购物车')


    def commodity_cat_number(self): #购物车——购物车商品数量
        self.shopping_cart()
        ele=self.get_text(self.CAT_NUMBER,page_name='获取购物车页面上的商品数量')
        self.delete_all()#删除
        return ele

    def shoppin_price(self):
        ele=self.get_text(self.SHANG_PIN,page_name='获取商品详情页商品价格')
        return ele

    def shopping_cart(self):#点击购物车按钮
        self.click(self.SHOPIN_CAT,page_name='进入购物车页面')
    def shopping_cart_price(self):#加入购物车、进入购物车
        self.click(self.ADD_CAT, page_name='点击加入购物车')
        self.click(self.SHOPIN_CAT,page_name='进入购物车页面')
    def shopping_cart_number(self):#购物车页面上的商品数量
        return self.get_text(self.CAT_NUMBER,page_name='获取购物车页面上的商品数量')
    def shopping_jaige(self):
        return self.get_text(self.CAT_JAIGE,page_name='获取购物车页面上的价格')


    def cart_account(self):#结算按钮
        self.click(self.CAT_SUM,page_name='点击结算按钮')
    def cart_account_jin(self,value):
        return self.get_attribute(self.CAT_SUM,value,page_name=f'获取状态')


    def check_all(self):#全选按钮
        self.click(self.QUAN_XUAN,page_name='点击全选按钮')

    def delete_all(self):#删除按钮
        self.click(self.PI_SHANCHU,page_name='点击删除按钮')
        self.click(self.PI_SHANCHU_QUE,page_name='点击确认删除按钮')
    #uname='测试',iphone='17601205856',dizhi='天津市/市辖区/和平区',xiangdizhi='卧龙小聚'
    def shipping_address(self,uname,iphone,dizhi,xiangdizhi):
        print(uname,iphone,dizhi,xiangdizhi)
        self.click(self.SHOU_ADD,page_name='点击添加按钮')
        self.input_element(self.SHOU_HUOREN,uname,page_name=f'输入收货人：{uname}')
        self.input_element(self.SHOU_IPHONE,iphone,page_name=f'收货人手机号：{iphone}')
        self.click(self.SHOU_DIZHI,page_name='点击地址')
        self.click(self.SHI,page_name='点击市')
        self.click(self.SHIQU,page_name='点击辖区')
        self.click(self.SHIQUDI ,page_name='点击区')
        self.input_element(self.SHOU_XIANGXI,xiangdizhi,page_name=f'收货人详细地址{xiangdizhi}')
        self.click(self.SHOU_BUTTON,page_name='点击提交按钮')
        self.click(self.SHOU_XUANZE, page_name='选择下单地址')

    def submit_order(self):
        self.click(self.TI_JIAODINGDAN,page_name='点击提交订单按钮')

    def payment_method(self):
        self.click(self.TI_ZHIFU,page_name='选择支付方式')



    def payment_success(self):
        self.get_text(self.ZHIFU_ZHUANGTAI,page_name=f'获取支付状态')

    def shopping_shuliang(self):#进入购物车-商品数量
        self.shopping_cart()
        return self.shopping_cart_number()




    def shopping_xunze(self):#选择地址-提交订单
        self.click(self.SHOU_XUANZE,page_name='选择收货地址')
        self.click(self.TI_JIAODINGDAN,page_name='点击提交订单按钮')
        self.click(self.SHOU_YE,page_name='创建订单成功跳到首页')


    def cart_account_jie(self):#结算-选择地址-提交订单-查看价格-
        self.cart_account()
        self.click(self.SHOU_XUANZE, page_name='选择收货地址')
        ele=self.get_text(self.TI_ZHIFU,page_name='获取支付价格')
        self.click(self.TI_JIAODINGDAN, page_name='点击提交订单按钮')
        return ele

    def cart_account_zhifu(self):#结算-选择地址-提交订单-支付成功
        self.cart_account()
        self.click(self.SHOU_XUANZE, page_name='选择下单地址')
        self.click(self.TI_JIAODINGDAN, page_name='点击提交订单按钮')
        self.click(self.TI_ZHIFU_WHEAT,page_name='选择微信支付方式')
        sleep(6)
        return self.get_text(self.ZHIFU_ZHUANGTAI,page_name=f'获取支付状态')



